package com.fw.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import com.fw.model.Ecommerce;
import com.fw.model.Order;
import com.fw.service.EcommerceService;
import com.fw.service.OrderService;
import com.fw.service.impl.EcommerceServiceImpl;
import com.fw.service.impl.OrderServiceImpl;
import com.fw.util.SessionUser;
import com.jfinal.core.Controller;

public class OrderController extends Controller {
	OrderService orderService = null;
	EcommerceService ecommerceService = null;
	public void toOrderListView() {
		renderJsp("/WEB-INF/jsp/order_list.jsp");
	}

	public void query_allOrderByPage() throws UnsupportedEncodingException {
		List<Object> par = new ArrayList<Object>();
		orderService = new OrderServiceImpl();
		StringBuffer sql = new StringBuffer();
		SessionUser sessionUser = getSessionAttr("sessionUser");
		if(sessionUser.getRid() != 5){
			sql.append(" and o.u_id = ?");
			par.add(sessionUser.getUserId());
		} 
		HttpServletRequest request = getRequest();
		request.setCharacterEncoding("utf-8");
		HttpServletResponse response = getResponse();
		response.setContentType("text/html;charset = utf-8");
		// 显示的起始索引
		int limitStart = Integer.parseInt(getPara("start").toString());
		// 显示的行数
		int limitLength = Integer.parseInt(getPara("length").toString());
		String draw = "0";
		draw = getPara("draw");
		int totalData = orderService.query_totalOrderByPageDao(sql.toString(),
				par);
		List<Order> rs = orderService.query_allOrderByPageDao(sql.toString(),
				limitStart, limitLength, par);
		Map<Object, Object> info = new HashMap<Object, Object>();
		info.put("data", rs);
		info.put("recordsTotal", totalData);
		info.put("recordsFiltered", totalData);
		info.put("draw", draw);
		renderJson(info);
	}
	public void toAddOrderView(){
		ecommerceService = new EcommerceServiceImpl();
		List<Ecommerce> ecs = ecommerceService.query_allEcommerceService();
		setAttr("ecommerces", ecs);
		renderJsp("/WEB-INF/jsp/add_order.jsp");
	}
	public void add_order() {
		Order order = getModel(Order.class);
		orderService = new OrderServiceImpl();
		Date date = new Date();
		order.put("submit_date", date);
		boolean flag = orderService.add_orderDao(order);
		String message = null;
		if (flag) {
			message = "添加成功";
			setAttr("message", message);
		} else {
			message = "添加失败";
			setAttr("message", message);
		}
		renderJsp("/WEB-INF/jsp/cue_message.jsp");
	}
	public void toUpdateOrderView(){
		Order order = getModel(Order.class);
		orderService = new OrderServiceImpl();
		Order o = orderService.query_orderByIdDao(order);
		ecommerceService = new EcommerceServiceImpl();
		List<Ecommerce> ecs = ecommerceService.query_allEcommerceService();
		setAttr("ecommerces", ecs);
		setAttr("order", o);
		renderJsp("/WEB-INF/jsp/update_order.jsp");
	}
}
